package com.ctshk.sapp.order.airexpress.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderPageDTO;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderPayInfoDTO;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderQueryDTO;
import com.ctshk.rpc.order.airexpress.req.*;
import com.ctshk.rpc.order.airexpress.service.IAirportExpressOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 机场快线订单表 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-23
 */
@Slf4j
@RestController
@RequestMapping("/airport/express/order")
@Api(value = "机场快线订单中心", tags = "机场快线订单中心")
public class AirportExpressOrderController extends BaseController {
    @DubboReference
    IAirportExpressOrderService airportExpressOrderService;

    @ApiOperation(value = "机场快线-订单中心-提交订单", notes = "机场快线-订单中心-提交订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "机场快线-订单中心-提交订单", operDesc = "机场快线-订单中心-提交订单")
    @PostMapping("/submit")
    public Result<Long> submit(@Valid @RequestBody AirportExpressOrderReq airportExpressOrderReq) {
        Long userId = getCurrentUser(getRequest()).getId();
        return airportExpressOrderService.submit(airportExpressOrderReq, userId);
    }

    @ApiOperation(value = "机场快线-订单中心-支付订单查询详情", notes = "机场快线-订单中心-支付订单查询详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "机场快线-订单中心-支付订单查询详情", operDesc = "机场快线-订单中心-支付订单查询详情")
    @GetMapping("/pay/info/{id}")
    public Result<AirportExpressOrderPayInfoDTO> paymentInfoById(@PathVariable("id") Long id) {
        return airportExpressOrderService.paymentInfoById(id);
    }

    @ApiOperation(value = "机场快线-订单中心-支付订单", notes = "机场快线-订单中心-支付订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "机场快线-订单中心-支付订单", operDesc = "机场快线-订单中心-支付订单")
    @PostMapping("/pay/{id}")
    public Result<Long> pay(@PathVariable("id") Long id, @Valid @RequestBody List<AirportExpressOrderCollectionReq> airportExpressOrderCollectionReqList) throws Exception {
        Long userId = getCurrentUser(getRequest()).getId();
        return airportExpressOrderService.payment(id, airportExpressOrderCollectionReqList, userId);
    }

    @ApiOperation(value = "机场快线-订单中心-取消订单", notes = "机场快线-订单中心-取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "机场快线-订单中心-取消订单", operDesc = "机场快线-订单中心-取消订单")
    @PostMapping("/cancel/{id}")
    public Result<Long> cancel(@PathVariable("id") Long id, @Valid @RequestBody AirportExpressOrderCancelReq airportExpressOrderCancelReq) {
        TokenUser currentUser = getCurrentUser(getRequest());
        return airportExpressOrderService.cancel(id, airportExpressOrderCancelReq, currentUser);
    }

    @ApiOperation(value = "机场快线-订单中心-查询订单", notes = "机场快线-订单中心-查询订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-查询订单", operDesc = "机场快线-订单中心-查询订单")
    @GetMapping("/query/{id}")
    public Result<AirportExpressOrderQueryDTO> query(@PathVariable("id") Long id) {
        return airportExpressOrderService.query(id);
    }

    @ApiOperation(value = "机场快线-订单中心-查询订单列表", notes = "机场快线-订单中心-查询订单列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-查询订单列表", operDesc = "机场快线-订单中心-查询订单列表")
    @PostMapping("/list")
    public Result<PageResponse<AirportExpressOrderPageDTO>> list(@RequestBody AirportExpressOrderListReq airportExpressOrderListReq) {
        airportExpressOrderListReq.setUserId(getCurrentUser(getRequest()).getId());
        PageResponse<AirportExpressOrderPageDTO> page = airportExpressOrderService.list(airportExpressOrderListReq);
        return Result.success(page);

    }

    @ApiOperation(value = "机场快线-订单中心-港铁测试接口", notes = "机场快线-订单中心-港铁测试接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "机场快线-订单中心-港铁测试接口", operDesc = "机场快线-订单中心-港铁测试接口")
    @PostMapping("/testMtr")
    public Result testMtr(@RequestParam("type") int type, @RequestBody List<String> reqStr) {
        return airportExpressOrderService.mtrTest(type, reqStr.get(0));
    }

    @ApiOperation(value = "机场快线-订单操作-更新联络人", notes = "机场快线-订单操作-更新联络人")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "机场快线-订单中心-更新联络人", operDesc = "机场快线-订单中心-更新联络人")
    @PostMapping("/contact/update")
    public Result<Integer> updateContact(@Valid @RequestBody AirportExpressOrderUpdateReq airportExpressOrderUpdateReq) {
        Long userId = getCurrentUser(getRequest()).getId();
        int num = airportExpressOrderService.updateContact(airportExpressOrderUpdateReq, userId);
        return Result.success(num);
    }

    @ApiOperation(value = "机场快线-订单操作-获取可退款金额", notes = "机场快线-订单操作-获取可退款金额")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-获取可退款金额", operDesc = "机场快线-订单中心-获取可退款金额")
    @GetMapping("/refund/amount/{id}")
    public Result<BigDecimal> getRefundAmount(@PathVariable("id") Long id) {
        BigDecimal refundAmount = airportExpressOrderService.getRefundAmount(id);
        return Result.success(refundAmount);
    }

    @ApiOperation(value = "机场快线-订单操作-确认退款", notes = "机场快线-订单操作-确认退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "机场快线-订单中心-更新联络人", operDesc = "机场快线-订单中心-更新联络人")
    @PostMapping("/confirm/refund/{id}/{refundId}")
    public Result<Long> confirmRefund(@PathVariable("id") Long id, @PathVariable("refundId") Long refundId) {
        Long userId = getCurrentUser(getRequest()).getId();
        return airportExpressOrderService.confirmRefund(id, refundId, userId);
    }
}
